﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using VirusCleaner.BugReport;
using VirusCleaner.IPlugin.Plugin;

//=========================================
//
// 作 者：LsongStudio
// 邮 箱：song940@gmail.com
// 博 客：http://lsongs.host.sk/
// 时 间：2010-11-24 18:37:56
//
//=========================================
namespace VirusCleaner.Process
{
    public partial class ProcessControl : UserControl,IUiElement
    {
        private ILogService _logging = null;

        public ProcessControl()
        {
            InitializeComponent();

#if DEBUG
            _logging = new LogService(true);
#else
            _logging = new LogService(false);
#endif
        }
        #region IUiElement 成员

        public int Init()
        {
            timerProcess.Start();
            return 0;
        }
        public void Active(bool active)
        {
            timerProcess.Enabled = active;
        }
        #endregion
        private void InitProcess()
        {
            foreach (ListViewItem item in lvProcess.Items)
            {
                try
                {
                    int pid = Convert.ToInt32(item.Tag);
                    System.Diagnostics.Process.GetProcessById(pid);
                }
                catch (ArgumentException)
                {
                    lvProcess.Items.Remove(item);
                }
            }
            var processlist = System.Diagnostics.Process.GetProcesses();

            foreach (var process in processlist)
            {
                ListViewItem findItemWithText = lvProcess.FindItemWithText(process.ProcessName);
                if (findItemWithText == null)
                {
                    var lvi = new ListViewItem
                    {
                        Tag = process.Id,
                        Text = process.ProcessName
                    };

                    lvi.SubItems.Add(process.Id.ToString());
                    lvi.SubItems.Add(process.Threads.Count.ToString());
                    lvi.SubItems.Add(process.HandleCount.ToString());
                    lvi.SubItems.Add(process.WorkingSet64.ToString());
                    try
                    {
                            lvi.Text = process.MainModule.ModuleName;
                            lvi.SubItems.Add(process.MainModule.FileVersionInfo.CompanyName);
                            lvi.SubItems.Add(process.MainModule.FileVersionInfo.FileDescription);
                            lvi.SubItems.Add(process.MainModule.FileVersionInfo.FileVersion);
                            lvi.SubItems.Add(process.MainModule.FileVersionInfo.FileName);
                    }
                    catch (Exception)
                    {
                        while(lvi.SubItems.Count < lvProcess.Columns.Count)
                        {
                            lvi.SubItems.Add(process.ProcessName);
                        }

                    }
                    lvProcess.Items.Add(lvi);
                }
                else
                {
                    int threadCount = Int32.Parse(findItemWithText.SubItems[2].Text);
                    if (threadCount != process.Threads.Count)
                    {
                        findItemWithText.SubItems[2].Text = process.Threads.Count.ToString();
                    }
                    int handleCount = Int32.Parse(findItemWithText.SubItems[3].Text);
                    if(handleCount != process.HandleCount)
                    {
                        findItemWithText.SubItems[3].Text = process.HandleCount.ToString();
                    }
                    findItemWithText.SubItems[4].Text = (process.WorkingSet64/1024).ToString("0,0K");
                }
            }
        }
        private void TimerProcessTick(object sender, EventArgs e)
        {
            InitProcess();
        }
        private void LvProcessItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            try
            {
                ListViewItem lvi;
                lvModels.Items.Clear();
                int pid = Convert.ToInt32(e.Item.Tag);
                var process = System.Diagnostics.Process.GetProcessById(pid);
                foreach (ProcessModule models in process.Modules)
                {
                    lvi = new ListViewItem(models.ModuleName);
                    lvi.SubItems.Add(models.FileVersionInfo.CompanyName);
                    lvi.SubItems.Add(models.FileVersionInfo.FileDescription);
                    lvi.SubItems.Add("0x" + models.BaseAddress.ToString("X8"));
                    lvi.SubItems.Add(models.FileVersionInfo.ProductVersion);
                    lvi.SubItems.Add(models.FileVersionInfo.FileName);

                    lvModels.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                _logging.WriteException("error:",ex);
            }
        }
        #region IPlugin 成员

        public string PluginName
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}